<?php

/**
 * XMLChart class
 *
 * Validate and parse XML document with the chart data to plot
 *
 * @name XMLChart
 * @author DiMoN
 * @copyright Dmitry Pavlov
 */

class XMLChart
{
    /**
     * XML Document with chart parameters
     *
     * @var DOMDocument
     */
    private $XMLDocument;

    /**
     * Root element
     *
     * @var DOMElement
     */
    private $Root;

    /**
     * Chart title
     *
     * @var string
     */
    public $Title;

    /**
     * Chart parameter name
     *
     * @var string
     */
    public $ParameterName;

    /**
     * Chart Axis X name
     *
     * @var string
     */
    public $NameX;

    /**
     * Chart Axis Y name
     *
     * @var string
     */
    public $NameY;

    /**
     * Data array for Charts which support multiple datasets (type 3)
     *
     * @var array
     */
    public $DataArray3D;

    /**
     * Data array for Charts which support only one dataset (type 2)
     *
     * @var array
     */
    public $DataArray2D;

    /**
     * Labels array
     *
     * @var array
     */
    public $LabelsArray;

    /**
     * Legend array
     *
     * @var array
     */
    public $LegendArray;

    /**
     * XMLChart constructor
     * Loads and validates XML document with the chart data
     *
     * @param string $xml - XML document
     * @throws Exception
     */
    public function __construct($xml)
    {
        $this->XMLDocument = new DOMDocument();
        $this->XMLDocument->preserveWhiteSpace = false;

        if (@$this->XMLDocument->loadXML($xml))
        {
            ini_set("track_errors", "On");

            if (@$this->XMLDocument->schemaValidate(Settings::$ChartDataXSDPath))
            {
                $this->parseChartDataXML();
            }
            else
            {
                throw new Exception("XML document with the event data is not valid: $php_errormsg", 102);
            }

            ini_set("track_errors", "Off");
        }
        else
        {
            throw new Exception("Impossible to load XML document with the event data", 101);
        }
    }

    /**
     * Parses content of the XML document
     *
     */
    private function parseChartDataXML()
    {
        $this->Root = $this->XMLDocument->documentElement;
        $this->Title = $this->Root->getAttribute("title");
        $this->ParameterName = $this->Root->getAttribute("parameter");
        $this->NameX = $this->Root->getAttribute("namex");
        $this->NameY = $this->Root->getAttribute("namey");

        $dataNode = $this->Root->childNodes->item(0);
        $labelsNode = $this->Root->childNodes->item(1);
        $legendNode = $this->Root->childNodes->item(2);

        $this->DataArray2D = array();
        $this->DataArray3D = array();
        $rowsNodeList = $dataNode->childNodes;
        foreach($rowsNodeList as $rowNode)
        {
            $rowArray = array();
            $colsNodeList = $rowNode->childNodes;
            foreach($colsNodeList as $colNode)
            {
                $rowArray[] = $colNode->getAttribute("value");
            }
            $this->DataArray3D[] = $rowArray;
        }

        if(count($this->DataArray3D) == 1)
        {
            $this->DataArray2D = $this->DataArray3D[0];
        }

        $this->LabelsArray = array();
        $labelsNodeList = $labelsNode->childNodes;
        foreach($labelsNodeList as $labelNode)
        {
            $this->LabelsArray[] = $labelNode->getAttribute("name");
        }

        $this->LegendArray = array();
        $legendNodeList = $legendNode->childNodes;
        foreach($legendNodeList as $legendItemNode)
        {
            $this->LegendArray[] = $legendItemNode->getAttribute("name");
        }
    }
}

?>